/**
 * <copyright>
 * Copyrigth 2007 Aurelien Moreau
 *
 * This file is part of ProcessShop.
 *
 * ProcessShop is free software; you can redistribute it and/or modify
 * it under the terms of the GNU Lesser General Public License as published by
 * the Free Software Foundation; either version 2 of the License, or
 * (at your option) any later version.
 * 
 * ProcessShop is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU Lesser General Public License for more details.
 * 
 * You should have received a copy of the GNU Lesser General Public License
 * along with ProcessShop; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
 * </copyright>
 *
 * $Id$
 */
package net.yienyien.process.folder.stdPatterns.impl;

import net.yienyien.process.bpd.BPDComponent;
import net.yienyien.process.bpd.BPDObject;
import net.yienyien.process.bpd.BpdPackage;
import net.yienyien.process.bpd.ControlFlowRelation;
import net.yienyien.process.folder.stdPatterns.StdPatternsPackage;
import net.yienyien.process.folder.stdPatterns.While;

import org.eclipse.emf.common.util.BasicEList;
import org.eclipse.emf.common.util.EList;
import org.eclipse.emf.ecore.EClass;

/**
 * <!-- begin-user-doc --> An implementation of the model object '<em><b>While</b></em>'.
 * <!-- end-user-doc -->
 * <p>
 * </p>
 *
 * @generated
 */
public class WhileImpl extends StdBPDPatternImpl implements While {

	public static final String NAME = "While";

	/**
	 * <!-- begin-user-doc --> <!-- end-user-doc -->
	 * @generated NOT
	 */
	protected WhileImpl() {
		super(NAME, BpdPackage.eINSTANCE.getXORMerge(), BpdPackage.eINSTANCE.getDataBasedXOR(), BpdPackage.eINSTANCE.getTask(), BpdPackage.eINSTANCE.getIntermediateEvent());
	}

	/**
	 * <!-- begin-user-doc --> <!-- end-user-doc -->
	 * @generated
	 */
	@Override
	protected EClass eStaticClass() {
		return StdPatternsPackage.Literals.WHILE;
	}

	@Override
	public EList<BPDObject> getContent(BPDObject source, BPDObject sink) {
		if (!isConnected(source, sink)) {
			return null;
		}

		EList<BPDObject> set = new BasicEList<BPDObject>();

		for (ControlFlowRelation r : sink.getOutputs()) {
			BPDObject o = r.getOutput();
			if (isConnected(o, source)) {
				set.add(o);
			}
		}

		if (set.size() > 1) {
			return null;
		}

		return set;
	}

	@Override
	public boolean verify(BPDComponent component) {
		if (component.getObjects().size() != 3
				|| component.getSource().getOutputs().size() != 1) {
			return false;
		}

		BPDObject x = null;
		for (BPDObject o : component.getObjects()) {
			if (!o.equals(component.getSource())
					&& !o.equals(component.getSink())) {
				x = o;
			}
		}

		if (x == null || x.getInputs().size() != 1
				|| x.getOutputs().size() != 1) {
			return false;
		}

		if (!component.getSink().equals(
				component.getSource().getOutputs().get(0).getOutput())) {
			return false;
		}

		if (!component.getSource().equals(x.getOutputs().get(0).getOutput())) {
			return false;
		}

		for (ControlFlowRelation r : component.getSink().getOutputs()) {
			if (r.getOutput().equals(x)) {
				return true;
			}
		}

		return false;
	}

} // WhileImpl
